home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-15 / preempt.zip / PREEMPT.C < prev   
Text File  |  1993-01-04  |  4KB  |  136 lines

  1.  
  2. /****************************************************************
  3. *********\
  4.  *  preempt.c  -- iRMX II preemption time benchmark.
  5.  *  Measures the time for 1 preemptive task switch + 1  * 
  6. non-preemptive task 
  7.  *  switch. Compiler: iC-286 V4.1 (LARGE model). Q4 1989, by R. P.
  8.  *  Kar
  9. \****************************************************************
  10. *********/
  11.  
  12. #include <stdio.h>
  13. #include <rmxc.h>
  14.  
  15. /* NOTE: 100,000 iterations takes about 35 minutes on a 16 MHz 386
  16. PC */
  17. #define MAX_LOOPS 100000L
  18.  
  19. /* Note: This is a CPU-dependent value. It must be set such that
  20.  * the execution time for this loop: for (j=0; j < ONE_TICK; j++)
  21. spare++
  22.  * is slightly longer than one iRMX sleep period. 
  23. */
  24. #define ONE_TICK 4200
  25.  
  26. unsigned       pri, status, i, spare, el_time;
  27. unsigned long  strt_sec, end_sec;
  28. selector       task1_t, task2_t, co_conn;
  29. unsigned long  count1, count2;
  30. float          preempt_time;
  31.  
  32. /* "union" used to decompose a pointer into segment:offset */
  33. typedef struct {unsigned offset; selector sel;} ptr_s;
  34. union { unsigned *pointer; ptr_s ptr; } ptr_u;
  35.  
  36. /* The lower priority task. It sits in delay loop waiting to be
  37. preempted. */
  38. void task1()
  39. {
  40.   unsigned loc_status;
  41.   for (count1 = 0; count1 < MAX_LOOPS; count1++)
  42.     for (i = 0; i < ONE_TICK; i++) ++spare;              /* Waste
  43. time */
  44.   printf("deleting task 1\n\n");
  45.   rqdeletetask(NULL, &loc_status);                       /* delete
  46. self */
  47. }
  48.  
  49. /* The higher priority task. When it goes to sleep (once in every
  50. loop) iRMX
  51.  * makes a non-preemptive switch to the other task; when the sleep
  52. period ends
  53.  * this task preempts the other task.
  54.  */
  55. void task2()
  56. {
  57.   unsigned loc_status;
  58.   for (count2 = 0; count2 < MAX_LOOPS; count2++)
  59.     /* When rqsleep is called, task switch to lower priority task
  60. happens.
  61.      * When 1 clock period is over, other task is preempted and
  62. control
  63.      * returns to the next line.
  64.      */
  65.     rqsleep(1, &loc_status);
  66.   printf("\ndeleting task 2\n");
  67.   rqdeletetask(NULL, &loc_status);                 /* delete self
  68. */
  69. }
  70.  
  71. /*************************  MAIN PROGRAM 
  72. *************************/
  73. main()
  74. {
  75.  
  76. printf("\nPreemption time benchmark\n   Each task runs %D
  77. times...\n\n",
  78.            MAX_LOOPS);
  79.  
  80. /* Measure execution time of task1 and task2 when they are executed
  81.  * serially (without task switching or preemption).
  82.  */
  83. strt_sec = rqgettime(&status);                   /* Start of timing
  84. period */
  85.   for (count1 = 0; count1 < MAX_LOOPS; count1++)
  86.     for (i = 0; i < ONE_TICK; i++) ++spare;
  87.   for (count2 = 0; count2 < MAX_LOOPS; count2++)
  88. end_sec = rqgettime(&status);                    /* End of timing
  89. period */
  90.  
  91. el_time = (unsigned)(end_sec - strt_sec);
  92.  
  93. printf("    Execution without premption & task switching took %u
  94. seconds\n",
  95.                el_time);
  96.  
  97. /* Place a pointer to any variable in union "ptr_u", so the data
  98. segment
  99.    of this program becomes known.
  100.  */
  101. ptr_u.pointer = &status;
  102.  
  103. /* Get main program's priority */
  104. pri = rqgetpriority (NULL, &status);
  105.  
  106. task1_t = rqcreatetask (pri+2, task1, ptr_u.ptr.sel, 0L, 512, 0,
  107. &status);
  108. if (status != 0) printf("rqcreatetask error\n");
  109.  
  110. task2_t = rqcreatetask (pri+1, task2, ptr_u.ptr.sel, 0L, 512, 0,
  111. &status);
  112.  
  113. strt_sec = rqgettime(&status);                  /* Start of timing
  114. period */
  115.  
  116. /* Set main program's priority below task 1,2 so they run to
  117. completion */
  118. rqsetpriority( (selector)0, pri+3, &status );
  119. rqsleep( 0, &status );
  120.  
  121. end_sec = rqgettime(&status);                  /* End of timing
  122. period */
  123.  
  124. /* Set main program back to initial priority */
  125. rqsetpriority( (selector)0, pri, &status );
  126. el_time = (unsigned)(end_sec - strt_sec) - el_time;
  127. preempt_time = ( (float)el_time / (float)MAX_LOOPS ) * 1000000.0;
  128. printf("     Preemption time + task switch time = %5.1f
  129. microseconds\n",
  130.                    preempt_time);
  131. dqexit(0);
  132. }
  133.  
  134.  
  135.  
  136.